package com.gouqi.mall.interceptor;


import com.auth0.jwt.interfaces.Claim;
import com.gouqi.mall.service.UserService;
import com.gouqi.mall.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 拦截器:拦截所有请求
 * @author:小费
 * @date 2019/06/14
 */
public class AuthenticationInterceptor implements HandlerInterceptor {

    @Autowired
    UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        System.out.println("---------------------拦截请求------------------");
        // 从 http 请求头中取出 token
        String token = httpServletRequest.getHeader("token");
        System.out.println("拦截到的token："+token);
//        //验证token是否正确
//        Map<String, Claim> stringClaimMap = TokenUtils.verifyToken(token);
//        if (stringClaimMap.size() == 0) {
//            System.out.println("拦截失败················");
//            httpServletRequest.setAttribute("result",false);
//        } else {
//            System.out.println("拦截成功··········");
//            System.out.println("token信息：账号---"+stringClaimMap.get("account").asString());
//            System.out.println("token信息：密码---"+stringClaimMap.get("password").asString());
//            System.out.println("token信息：姓名---"+stringClaimMap.get("name").asString());
//            System.out.println("token信息：年龄---"+stringClaimMap.get("age").asString());
//            httpServletRequest.setAttribute("result",true);
//        }
        return true;
    }
}

